Total Complexity | 10 |
Total Lines | 33 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import Markov from './Markov'; |
||
4 | export default class BaseGenerator { |
||
5 | // eslint-disable-next-line sonarjs/cognitive-complexity |
||
6 | constructor(fatum) { |
||
7 | this.fatum = fatum; |
||
8 | if (this.constructor.model) { |
||
9 | const { type } = this.constructor.model; |
||
10 | |||
11 | if (type === 'markov') { |
||
12 | this._markov = new Markov(this.constructor.model, this.constructor.safeLength); |
||
13 | } |
||
14 | |||
15 | if (type === 'merged_types') { |
||
16 | this._generators = {}; |
||
17 | for (const typeName of Object.keys(this.constructor.model.types)) { |
||
18 | const model = this.constructor.model.types[typeName]; |
||
19 | |||
20 | if (model.type === 'markov') { |
||
21 | this._generators[typeName] = new Markov(model); |
||
22 | } |
||
23 | |||
24 | if (model.type === 'static') { |
||
25 | this._generators[typeName] = new Static(model); |
||
26 | } |
||
27 | } |
||
28 | } |
||
29 | } |
||
30 | } |
||
31 | |||
32 | generateByModel(type, ...params) { |
||
33 | if (this._markov) return this._markov.generate(type, ...params); |
||
|
|||
34 | if (this._generators) return this._generators[type].generate(...params); |
||
35 | } |
||
36 | } |
||
37 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.